package com.lori.netshop.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lori.netshop.pojo.TbContent;
import com.lori.netshop.service.ContentService;
import com.lori.netshop.util.HttpClientUtil;
import com.lori.netshop.util.NetshopResult;
@Controller
public class ContentServiceimp implements ContentService{
	//获得资源文件数据
	@Value("${BASEURI}")
	private String BASEURI;
	@Value("${CONTENTSURI}")
	private String CONTENTSURI;
	@Value("${PICBASEURI}")
	private String PICBASEURI;
	@Override
	public String findContents(long categoryId) {
		//通过httpclient调用restful的接口获得数据
		String contents = HttpClientUtil.doGet(BASEURI+CONTENTSURI+"/"+categoryId);
		//创建map用于存放需要返回的数据
		List<Map> resultList = new ArrayList<Map>();
		//将字符串转为netshopresult类型
		NetshopResult netshopresult = NetshopResult.formatToList(contents, TbContent.class);
		if(netshopresult != null && netshopresult.getStatus() == 200){
			List<TbContent> contentList = (List<TbContent>) netshopresult.getData();
			if(contentList != null && contentList.size()>0){
				for(TbContent tbContent:contentList){
					Map resultmap = new HashMap();
					resultmap.put("src", PICBASEURI+tbContent.getPic());
					resultmap.put("height", 240);
					resultmap.put("width", 670);
					resultmap.put("srcB", PICBASEURI+tbContent.getPic2());
					resultmap.put("widthB", 240);
					resultmap.put("heightB", 670);
					resultmap.put("href",PICBASEURI+tbContent.getUrl() );
					resultmap.put("alt",tbContent.getSubTitle() );
					resultList.add(resultmap);
				}
			}
		}
		ObjectMapper mapper = new ObjectMapper();
		String json = "";
		try {
			json = mapper.writeValueAsString(resultList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}

}
